package com.cmcc.aregister.ui.activity;

import java.util.HashMap;
import java.util.Map;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.method.ScrollingMovementMethod;
import android.widget.ImageView;
import android.widget.TextView;
import com.cmcc.aregister.R;
import com.cmcc.aregister.cs.constant.Arguments;
import com.cmcc.aregister.cs.constant.GlobalSetting;
import com.cmcc.aregister.cs.http.HttpAsyncTask;
import com.cmcc.aregister.cs.model.Disease;
import com.cmcc.aregister.cs.util.PotoalParcelable;
import com.cmcc.aregister.cs.util.Utils;

/**
 * 疾病详情页
 */
public class DiseaseDetailActivity extends ParentActivity {
	// ------view------
	// ------parameter------
	private Disease disease;

	// ------------

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		init(R.layout.p_disease_detail);
	}

	@Override
	protected void setupData() {
		Map<String, Object> httpMap = ((PotoalParcelable) getIntent()
				.getParcelableExtra(Arguments.INTENT_TO_NEXT_PAGE)).map;
		disease = (Disease) httpMap.get(Arguments.ACTIVITY_JUMP_DISEASE);
		sendGetDiseaseDetailTask(disease.ill_id);
	}

	@Override
	protected void setupView() {
		((ImageView) findViewById(R.id.title)).setBackgroundResource(R.drawable.disease_detail);
		((TextView) findViewById(R.id.name)).setText(disease.ill_name);
		((TextView) findViewById(R.id.illness_symptoms))
				.setMovementMethod(ScrollingMovementMethod.getInstance());
		((TextView) findViewById(R.id.illness_cause))
				.setMovementMethod(ScrollingMovementMethod.getInstance());
		((TextView) findViewById(R.id.medical_department))
				.setMovementMethod(ScrollingMovementMethod.getInstance());
		findViewById(R.id.medical_department).setSelected(true);
	}

	public void setView() {
		((TextView) findViewById(R.id.illness_desc)).setText(disease.ill_intro);
		((TextView) findViewById(R.id.illness_symptoms))
				.setText(disease.ill_symptoms);
		((TextView) findViewById(R.id.illness_cause))
				.setText(disease.ill_cause);
		((TextView) findViewById(R.id.medical_department))
				.setText(disease.relation_dep);
	}

	/**
	 * 获取疾病信息
	 */
	private HttpAsyncTask sendGetDiseaseDetailTask;

	/**
	 * 
	 * @param ill_id
	 *            疾病ID
	 */
	private void sendGetDiseaseDetailTask(String ill_id) {
		logger.d("----------sendGetDiseaseDetailTask----------");
		Map<String, String> parameterMap = new HashMap<String, String>();
		parameterMap.put("ill_id", ill_id);
		String getDiseaseDetailUrl = Utils.changeUrl(
				Arguments.SERVER_URL_GET_DISEASE_DETAIL, parameterMap);
		sendGetDiseaseDetailTask = new HttpAsyncTask(HttpAsyncTask.GET,
				GlobalSetting.XML_GET_DISEASE_DETAIL, getDiseaseDetailUrl,
				HttpAsyncTask.CACHE_TYPE_SAVE, false);
		sendGetDiseaseDetailTask.setUIHandler(sendGetDiseaseDetailTaskHandler);
		mHttpManager.execute(sendGetDiseaseDetailTask);
	}

	@SuppressWarnings("unchecked")
	Handler sendGetDiseaseDetailTaskHandler = new Handler() {
		public void handleMessage(Message msg) {
			logger.d("----------sendGetDiseaseDetailTaskHandler----------");
			switch (msg.what) {
			case HttpAsyncTask.TASK_NOTIFY_RETURN_DATA:
				dismissProgressDialog();
				if (msg.obj != null) {
					Map<String, Object> httpMap = (Map<String, Object>) msg.obj;
					Disease disease_1 = (Disease) httpMap
							.get(Arguments.XML_DISEASE);
					disease.ill_intro = disease_1.ill_intro;
					disease.ill_cause = disease_1.ill_cause;
					disease.ill_symptoms = disease_1.ill_symptoms;
					disease.relation_dep = disease_1.relation_dep;
					setView();
				} else {
				    showSingleAlertDlg(getString(R.string.notice),
	                    getString(R.string.read_fail),
	                    getString(R.string.close));
				}
				break;
			case HttpAsyncTask.TASK_NOTIFY_CONNECTION_FAIL:
				dismissProgressDialog();
				  showSingleAlertDlg(getString(R.string.notice),
	                    getString(R.string.read_fail),
	                    getString(R.string.close));
				break;
			}
		}
	};

	@Override
	protected void setTaskHandlerNull() {
		if (sendGetDiseaseDetailTask != null) {
			sendGetDiseaseDetailTask.setTaskBindingHandler(null);
		}
	}
}